<?php


function getImages($path, $galleryName) {

  $files = array();
   $fileNames = array();
   $i = 0;
   
   if (is_dir($path)) {
       if ($dh = opendir($path)) {
           while (($file = readdir($dh)) !== false) {
               if ($file == "." || $file == ".." || $file == ".DS_Store") continue;
               $fullpath = $path . "/" . $file;
               $fkey = strtolower($file);
               while (array_key_exists($fkey,$fileNames)) $fkey .= " ";
               $a = stat($fullpath);
               $files[$fkey]['size'] = $a['size'];
               if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-";
               else if ($a['size'] > 1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K";
               else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb";
               else $files[$fkey]['sizetext'] = $a['size'] . " bytes";
               $files[$fkey]['name'] = $file;
               $files[$fkey]['type'] = filetype($fullpath);
               $fileNames[$i++] = $fkey;
           }
           closedir($dh);
       } else die ("Cannot open directory:  $path");
   } else die ("Path is not a directory:  $path");
   sort($fileNames,SORT_STRING);
   $sortedFiles = array();
   $i = 0;
   foreach($fileNames as $f) $sortedFiles[$i++] = $files[$f];
   
   
   echo "<script type=\"text/javascript\">\n";
	echo "var $galleryName = new Array(\n";

   
   foreach ($sortedFiles as $file) {
   	
	// get image sizes 
	list($width, $height, $type, $attr) = getimagesize("$path/$file[name]", &$info);
	$size = $file[sizetext];
	$iptc = iptcparse($info["APP13"]);
	// iptc  info
	$iptc = iptcparse($info["APP13"]);
	$title = $iptc['2#005'][0];
	$description = $iptc['2#120'][0];
	$description = str_replace("\r", "<br/>", $description);
	$description = addslashes($description);
	$keywords = $iptc['2#025'][0];
	$author = $iptc['2#080'][0];
	$copyright = $iptc['2#116'][0];
	
	echo "new Array('$path/$file[name]', '$width', '$height', '$size', '$title', '$author', '$copyright', '$description'), \n";
	}
	echo "new Array('', '') \n";
	echo ");\n";
	echo "</script> \n";

}



?>



